<?php
       namespace App\Http\Controllers\home;
       use App\Http\Controllers\Controller;
       use Session;
       use App\Model\User;
       use Illuminate\Http\Request;
       use Illuminate\Support\Facades\Input;
       use Illuminate\Support\Facades\Validator;
       use Crypt;
       use Libs;
       class OrderController extends Controller{
	     public $phone_num="";
              public function order_info($name){
		    $selectData = new User();
		     if($selectData->check_session()){
                        $where['uid']= Session::get('u_id');
                        $data=$selectData->select_data("mo_user",$where);
                        return view('home.order_info.order_list',['name'=>$name,'data'=>$data]);
                   }else{
                        return redirect("/");
                   }
		  
               }
	      public function info_list(Request $request){
		  $selectData = new User();
		  if($request->session()->has('u_id')){
		  $where['uid']= $request->session()->get('u_id');
		  $data= $selectData->select_data("mo_user",$where);
		  $whereData['pid']=0;
                  $linData=$selectData->linkage('mo_region',$whereData);
		  if($data->u_r_city){
			$u_r_city= $selectData->select_data("mo_region",['r_city'=>$data->u_r_city]);
			$u_r_city=$u_r_city->r_id;
		  }else{
			$u_r_city="";
		   } 
	          
		  return view('home.order_info.info_list',array('linData'=>$linData,'data'=>$data,'u_r_city'=>$u_r_city));
		  }else{
		  
                       return redirect("/");
		}
	      }
	     public function change_city(Request $request){
		 $cate_id= $request->input('r_id');
		 $whereData['pid']=$cate_id;
		 $selectData = new User();
	         $linData=$selectData->linkage('mo_region',$whereData);
		 if(count($linData)){
		 	return array('success'=>true,'data'=>$linData);	 
		}
	      }
	      public function change_update(Request $request){
		   if($request->session()->has('u_name')){
			if($request->input('u_name')== $request->session()->get('u_name')){
			 $this->validate($request, [
                          'u_name'    =>'required|max:30',
                          'u_sex'=>'required',
                         'u_desc'=>'required|min:20|max:30',
                  	 ],
                     	 [
                           'u_name.required'       => '用户名必须填写',
                           'u_name.max'            => '用户名最多填写30个字符（一个中文占用两个字符）',
                           'u_sex.required'   => '性别必须选择',
                          'u_desc.required'=>'个人描述必须填写',
                          'u_desc.min'=>'个人描述至少20个字符',
			  'u_desc.max'=> '个人描述最好30个字符'

                 	]
         		 );
			}else{
			   $this->validate($request, [
                          'u_name'    =>'required|max:30|unique:mo_user',
                          'u_sex'=>'required',
                         'u_desc'=>'required|min:20|max:30',
                         ],
                         [
                           'u_name.required'       => '用户名必须填写',
                           'u_name.max'            => '用户名最多填写30个字符（一个中文占用两个字符）',
			   'u_name.unique'         => '用户名已存在',
                           'u_sex.required'   => '性别必须选择',
                          'u_desc.required'=>'个人描述必须填写',
                          'u_desc.min'=>'个人描述至少20个字符',
			  'u_desc.max'=> '个人描述最好30个字符'

                        ]
                         );

			}
		   }else{
                       return redirect("/");
		    }
		    $selectData = new User();
		    $where['uid']= $request->session()->get('u_id');
		    if($request->input('u_r_city') == -1 || $request->input('u_r_province') == -1){
			 
		    $data['u_name']= $request->input('u_name');
		    $data['u_sex']= $request->input('u_sex');
		    $data['u_desc']= $request->input('u_desc');
		   }else{
		   $u_r_city= $selectData->select_data("mo_region",['r_id'=>$request->input('u_r_city')]);
		   $u_r_province= $selectData->select_data("mo_region",['r_id'=>$request->input('u_r_province')]);
		   //var_dump($u_r_province);
		    $data['u_name']= $request->input('u_name');
		    $data['u_sex']= $request->input('u_sex');
		    $data['u_r_city']= $u_r_city->r_city;
		    $data['u_r_province']=$u_r_province->r_city;
		    $data['u_desc']= $request->input('u_desc');
		   }
		    $selectData->updataData('mo_user',$where,$data);
                    $request->session()->put('u_name',$request->input('u_name'));
		    return redirect("home/info");  
	
		}
	     public function change_image(Request $request){
		    $selectData = new User();
		    if($selectData->check_session()){ 
                    	$where['uid']= $request->session()->get('u_id');
                    	$data=$selectData->select_data("mo_user",$where);
		    	return view("home.order_info.image_list")->with('data',$data);
		   }else{
		 	return redirect("/");
		   }
		}
	     public function add_image(Request $request){
	             $file = $request->file('u_portrait');
		    if($file){
			   $allowed_extensions = ["png", "jpg", "gif","jpeg","JPG","PNG","GIF","JPEG"];
			   $destinationPath = 'admin_style/uploads/';
			  $extension = $file -> getClientOriginalExtension(); //4、获取上传图片的后缀名
			  if(in_array($extension,$allowed_extensions)){
          		  $data['u_portrait']= md5(time()).random_int(5,5).".".$extension;//5、重新命名上传文件名字
        	  	  $file->move($destinationPath,$data['u_portrait']);
		       	  $selectData = new User();
			  $res=$selectData->updataData('mo_user',array('uid'=>$request->session()->get('u_id')),$data);
			  if($res){
				   $title = array('content'=>'头像更改成功','url'=>'order_info/账号设置');
                        	   return view('admin.parent_alert',['data'=>$title]);
			  }
                       
       	   	    }else{
          		$title = array('content'=>'图片格式不正确','url'=>'change_image');
         	 	return view('admin.alert',['data'=>$title]);
                       }

		
			}
					
		}
	   public function change_pass(Request $request){
		  $selectData = new User();
		  if($selectData->check_session()){
                        $where['uid']= $request->session()->get('u_id');
                        $data=$selectData->select_data("mo_user",$where);
                        return view("home.order_info.pass_list")->with('data',$data);
                   }else{
                        return redirect("/");
                   }

	   }
	   public function add_pass(Request $request){
		$selectData = new User();
                  if($selectData->check_session()){
                        $where['uid']= $request->session()->get('u_id');
                        $data=$selectData->select_data("mo_user",$where);
			if($request->input('mpass')){
			 if(Crypt::decrypt($data->u_password) != $request->input('mpass')){
			  $title = array('content'=>'原密码不正确','url'=>'change_pass');
                          return view('admin.alert',['data'=>$title]);
		          }  
			 }
			$addData['u_password']=Crypt::encrypt($request->input('newpass'));
			$res=$selectData->updataData("mo_user",$where,$addData);
			if($res){    
			  $title = array('content'=>'密码修改成功','url'=>'change_pass');
                          return view('admin.alert',['data'=>$title]);
			}else{
			  $title = array('content'=>'密码修改失败','url'=>'change_pass');
                          return view('admin.alert',['data'=>$title]);
			 }
			   
                   }else{
                        return redirect("/");
                   }
	
	  }
	 public function bind_phone(Request $request){
		$selectData = new User();
		if($selectData->check_session()){
                    $where['uid']= $request->session()->get('u_id');
                    $data=$selectData->select_data("mo_user",$where);
		    if($data->u_phone){
	               $data->u_phone=substr_replace($data->u_phone,'****',3,4);  
		    }
		    return view("home.order_info.binding_phone",['data'=>$data,"errors"=>""]);
                   }else{
                      return redirect("/");
                   }   
	      }
	 public function get_phone(Request $request){
		$result=$this->check_yzm($request);
		if($result->fails()){
		    return response()->json(array(
                        'success' => false,
                        'errors' => $result->getMessageBag()->toArray() ));
		}else{
		    $randnumber=rand(100000,999999);
		    $reslut=$this->sendTemplateSMS($request->input('u_phone'),array($randnumber,1),240793);
		    if($result != NULL){
		     $request->session()->put('phone_num',$request->input('u_phone'));
		     $request->session()->put("code_number",$randnumber);
                     return response()->json(array(
                        'success' => true,
                        'errors'  => array(),
                        ));
            
		    }

		}
               }   
         public function check_yzm( $request){
		$selectData = new User();
		if($selectData->check_session()){
			 $data['u_phone']=$request->input('u_phone');
		         $rules = [
                         'u_phone'    =>'required|regex:/^1[34578]\d{9}$/',
                        ];

                        $message = [
                            'u_phone.required'  =>  '手机号必须填写',
                           'u_phone.regex'     =>  '手机格式不正确',
                       ];
		    $validator = Validator::make($data,$rules,$message );
		    return $validator;
			 
                   }else{
                      return redirect("/");
                   }   
	       }
	   public function sendTemplateSMS($to,$datas,$tempId){
              $accountSid= 'aaf98f8947f222a30147fb4668c60dfc';
              $accountToken= 'a1b6e7ebc5dc4259a599c185b987bc79';
              $appId='8a216da8623c38fd016241748fe202e7';
              $serverIP='app.cloopen.com';
              $serverPort='8883';
              $softVersion='2013-12-26';
	      $rest =new Libs($serverIP,$serverPort,$softVersion);
              $rest->setAccount($accountSid,$accountToken);
              $rest->setAppId($appId);
              $result = $rest->sendTemplateSMS($to,$datas,$tempId);
              return $result;

        }
	public function create_phone(Request $request){
		$selectData = new User();
                if($selectData->check_session()){
                    $where['uid']= $request->session()->get('u_id');
                    $data=$selectData->select_data("mo_user",$where);
		    if($data->u_phone){
	               $data->u_phone=substr_replace($data->u_phone,'****',3,4);  
		    }
		      $result=$this->check_yzm($request);
		      if($result->fails()){
			 $errorData= $result->getMessageBag()->toArray();
                         return view("home.order_info.binding_phone", ['data'=>$data,'errors'=>$errorData['u_phone'][0],'old'=>$request->input('u_phone')]);
			} 	
		     if( $request->input('u_phone') != $request->session()->get("phone_num")|| $request->session()->get("code_number") != $request->input('getCode')){
				return view("home.order_info.binding_phone", ['data'=>$data,'errors'=>"手机验证错误",'old'=>$request->input('u_phone')]);
			}
		     
		    $numberData=$selectData->select_data("mo_user",['u_phone'=>$request->input('u_phone')]);
		    if(count($numberData)){
		     if($request->session()->get('u_id') != $numberData->uid){
			$res=$selectData->del_data('mo_user',['uid'=>$numberData->uid]);
			if($res){
			   $reslut=$selectData->updataData("mo_user",['uid'=>$request->session()->get('u_id')],['u_phone'=>$request->input('u_phone')]);
			   if($result){
				$title = array('content'=>'手机绑定成功','url'=>'bind_phone');
                                return view('admin.alert',['data'=>$title]);

			    }
			}
		    }else{
                       $title = array('content'=>'手机号码已存在','url'=>'bind_phone');
                        return view('admin.alert',['data'=>$title]);
		    }

		    }else{
                           $result=$selectData->updataData("mo_user",['uid'=>$request->session()->get('u_id')],['u_phone'=>$request->input('u_phone')]);
                            if($result){
                                 $title = array('content'=>'手机绑定成功','url'=>'bind_phone');
                                 return view('admin.alert',['data'=>$title]);
                                 
                             }

           }
	  }else{
		return redirect("/");
	   }
	}
	 public function forget_password(Request $request){
		 $selectData = new User();
                  if($selectData->check_session()){
		    
                    return view("home.order_info.forget_password");
                   }else{
                      return redirect("/");
                   }

	}
	public function find_password(Request $request){
		$this->validate($request, [
                          'u_phone'    =>'required|regex:/^1[34578]\d{9}$/',
                         ],
                         [
                           'u_phone.required'       => '手机号必须写',
                           'u_phone.regex'            => '手机格式不正确',
                        ]);
		$selectData = new User();
		$where['uid']= $request->session()->get('u_id');
                $data=$selectData->select_data("mo_user",$where);
		if($data->u_phone != $request->input('u_phone')){
		$request->session()->put('u_phone',$request->input('u_phone'));
		   $title = array('content'=>'手机号不匹配','url'=>'forget_password');
                  return view('admin.alert',['data'=>$title]);
		}
		return view("home.order_info.reset_pass");
	   }
	 // 密码重置代码
	 public function rest_password(Request $request){
	 	$selectData = new User();
                  if($selectData->check_session()){
                    $where['uid']= $request->session()->get('u_id');
		    $addData['u_password']=Crypt::encrypt($request->input('newpass'));
		     $res=$selectData->updataData("mo_user",$where,$addData);
                        if($res){
                          $title = array('content'=>'密码重置成功','url'=>'change_pass');
                          return view('admin.alert',['data'=>$title]);
                        }else{
                          $title = array('content'=>'密码重置失败','url'=>'change_pass');
                          return view('admin.alert',['data'=>$title]);
                         }

                   }else{
                      return redirect("/");
                   }  
	 }
	// 渲染添加页面
	public function shopping_address(){
		$selectData = new User();
                  if($selectData->check_session()){
		      $data=$selectData->linkconent("mo_area",['parent_id'=>'0']);
                      return view('home.order_info.shopping_address',['data'=>$data]);
                   }else{
                      return redirect("/");
                   }

	 }
	public function selectd_address(Request $request){
		$cat_id = $request->input('r_id');
		$selectData = new User();
		if($selectData->check_session()){
                      $data=$selectData->linkconent("mo_area",['parent_id'=>$cat_id]);
                       if(count($data)){
                        return array('success'=>true,'data'=>$data);
                        }

                   }else{
                      return redirect("/");
                   }

	 }
		
       }
?>

